package com.xsz.opennlp.demo1;

import opennlp.tools.postag.POSModel;
import opennlp.tools.postag.POSTaggerME;
import opennlp.tools.sentdetect.SentenceDetectorME;
import opennlp.tools.sentdetect.SentenceModel;
import opennlp.tools.tokenize.TokenizerME;
import opennlp.tools.tokenize.TokenizerModel;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.Map;
//import org.opennlp.tools.text.*;

public class Test {

    private static String text = "Java is a programming language and computing platform first released by Sun Microsystems in 1995. It has evolved from humble beginnings to power a large share of today’s digital world, by providing the reliable platform upon which many services and applications are built. New, innovative products and digital services designed for the future continue to rely on Java, as well.";

    //句子分析
    public void sentenceDetectionTest() throws IOException {
        Path path = Paths.get("src",  "main","resources", "en-sent.bin");
        InputStream is = new FileInputStream(path.toFile());
        SentenceModel model = new SentenceModel(is);
        SentenceDetectorME sdetector = new SentenceDetectorME(model);
        String[] sentences = sdetector.sentDetect(text);
        for (String sentence : sentences) {
            System.out.println(sentence);
        }
    }

    public static void main(String[] args) throws IOException {
        System.out.println(System.getProperty("user.dir"));
        Test test=new Test();
        test.sentenceDetectionTest();
    }
// 词性分析
    public void posTagTest() throws IOException {
        String text = "Java is a programming language and computing platform first released by Sun Microsystems in 1995. ";
        Path path = Paths.get("src", "main", "resources", "en-sent.bin");
        InputStream is = new FileInputStream(path.toFile());
        SentenceDetectorME sdetector = new SentenceDetectorME(new SentenceModel(is));
        String[] sentences = sdetector.sentDetect(text);
        for (String sentence : sentences) {
            Path tokenPath = Paths.get("src", "main", "resources", "en-token.bin");
            InputStream tokeInputStream = new FileInputStream(tokenPath.toFile());
            TokenizerME tokenizer = new TokenizerME(new TokenizerModel(tokeInputStream));
            String[] tokens = tokenizer.tokenize(sentence);
            Path posPath = Paths.get("src", "main", "resources", "en-pos-perceptron.bin");
            InputStream posInputStream = new FileInputStream(posPath.toFile());
            POSModel posModel = new POSModel(posInputStream);
            POSTaggerME posTagger = new POSTaggerME(posModel);
            String[] tagArray = posTagger.tag(tokens);
            for (int i = 0; i < tokens.length; i++) {
                System.out.printf("%s -- %s%n", tokens[i], tagArray[i]);
            }
        }
    }

    // 分词
    public void getWorldTest() throws IOException {
        String text = "Java is a programming language and computing platform first released by Sun Microsystems in 1995. ";
        Path path = Paths.get("src", "main", "resources", "en-sent.bin");
        InputStream is = new FileInputStream(path.toFile());
        SentenceDetectorME sdetector = new SentenceDetectorME(new SentenceModel(is));
        String[] sentences = sdetector.sentDetect(text);
        for (String sentence : sentences) {
            Path tokenPath = Paths.get("src", "main", "resources", "en-token.bin");
            InputStream tokeInputStream = new FileInputStream(tokenPath.toFile());
            TokenizerME tokenizer = new TokenizerME(new TokenizerModel(tokeInputStream));
            String[] tokens = tokenizer.tokenize(sentence);
//            Path posPath = Paths.get("src", "main", "resources", "en-pos-perceptron.bin");
//            InputStream posInputStream = new FileInputStream(posPath.toFile());
//            POSModel posModel = new POSModel(posInputStream);
//            POSTaggerME posTagger = new POSTaggerME(posModel);
//            String[] tagArray = posTagger.tag(tokens);
            for (int i = 0; i < tokens.length; i++) {
                System.out.println(tokens[i]);
            }
        }
    }


    // 识别菜单
    public void getMenuTest() throws IOException {
        //用户输入内容
        String text = "Java is a programming language user and computing  platform first reletems in 1995. ";

        //菜单
        Map<String, String> dictionary = new HashMap<>();
        dictionary.put("user", "a\\b\\c");
        dictionary.put("edit", "\\d");

        Path path = Paths.get("src", "main", "resources", "en-sent.bin");
        InputStream is = new FileInputStream(path.toFile());
        SentenceDetectorME sdetector = new SentenceDetectorME(new SentenceModel(is));
        String[] sentences = sdetector.sentDetect(text);
        for (String sentence : sentences) {
            Path tokenPath = Paths.get("src", "main", "resources", "en-token.bin");
            InputStream tokeInputStream = new FileInputStream(tokenPath.toFile());
            TokenizerME tokenizer = new TokenizerME(new TokenizerModel(tokeInputStream));
            String[] tokens = tokenizer.tokenize(sentence);
            String menuPath = "";
            for (int i = 0; i < tokens.length; i++) {

                //开始查找菜单
                String menuName=tokens[i];

                if (dictionary.containsKey(menuName)) {
                    menuPath += dictionary.get(menuName);
                    System.out.println(menuName);
                } else {
//                    System.out.println("无法识别的菜单名称！");
                }

                //结束查找菜单
            }
            System.out.println("菜单path：" + menuPath);
        }
    }
// -- 结果
// Java -- NNP
// is -- VBZ
// a -- DT
// programming -- NN
// language -- NN
// and -- CC
// computing -- NN
// platform -- NN
// first -- JJ
// released -- VBN
// by -- IN
// Sun -- NNP
// Microsystems -- NNPS
// in -- IN
// 1995 -- CD
// . -- .
    }


//-- 结果
// Java is a programming language and computing platform first released by Sun Microsystems in 1995.
// It has evolved from humble beginnings to power a large share of today’s digital world, by providing the reliable platform upon which many services and applications are built.
// New, innovative products and digital services designed for the future continue to rely on Java, as well.


